<?php
$Current_Origen = 0;
$MYT = $GLOBALS['MYT'];
// Vars
if (!isset($_POST['TRANSTYPE'])) {
    if (!isset($_GET['TRANSTYPE'])) {
        $_POST['TRANSTYPE'] = 1;
    } else {
        $_POST['TRANSTYPE'] = $_GET['TRANSTYPE'];
    }
}
if (!isset($_POST['TTime'])) {
    $_POST['TTime'] = date("Y-m-d");
}
if (!isset($_POST['VType'])) {
    $_POST['VType'] = 1;
}
$rf = 0;
$QCorr = null;
// Total Vars
$TCAR = 0;
$PCNT = 0;
$TAVGRate = 0;
$TSUMTax = 0;
$TSUMProm = 0;
$TSUMSent = 0;
$TSUMReceiv = 0;

form_pr('TRANSProperties', '', '', 'post');
mn_field('hidden', 'ED', 'POOL', '', '', '', '', '');
?>
<script charset="utf-8" type="text/javascript" src="_response/bin.php?get=Console2.js"></script>
<table width="100%"  border="0" cellpadding="3" cellspacing="1" bgcolor="#B8C994">
    <tr bgcolor="#97C0C5">
        <td bgcolor="#99CC99">
            <table width="100%" border="0" cellspacing="1" cellpadding="0">
                <tr>
                    <td width="9%"> ServiÇo:            </td>
                    <td width="9%"><?php
$Opc = "Transferencias;Exchange#1;2";
mn_field('hselect', 'ED', 'TRANSTYPE', $_POST['TRANSTYPE'], $Opc, '', '', '"
	onChange="document.TRANSProperties.action=\'_response/call.php?get=Operative/viewdomaintransferresult\';
	subform(\'TRANSProperties\',\'Subcontents\');');
?></td>
                    <td width="35%"><div id="TransFilters"><?php include('viewdomaintransfermode.php'); ?></div></td>
                    <td width="45%" align="right">
                        <?php
                        echo '#';
                        mn_field('text', 'ED', 'SumCard', 0, '10', '4', '', 'INPNUM" readonly="');
                        echo ' Recebido:';
                        mn_field('text', 'ED', 'SumEnviado', 0.00, '20', '8', '', 'INPNUM" readonly="');
                        echo ' A Enviar:';
                        mn_field('text', 'ED', 'SumValues', 0.00, '20', '8', '', 'INPNUM" readonly="');
                        mn_field('button', 'ED', 'PRSVCX', 'Transmitir', '100', '38', 'False', 'SUBMIT"
	onClick="document.TRANSProperties.action=\'_response/rsproxy.php?pr=trm_makedomaintrm\';
	TransVal(1);');
                        ?></td>
                </tr>
            </table></td>
    </tr>
    <tr bgcolor="#EEF5F9">
        <td valign="top" bgcolor="#EEF5F9">Data: <?php
                        mn_field('date', 'ED', 'TTime', $_POST['TTime'], 'TRANSProperties', '8', '', '');
                        $Qur = "Por Transmitir;Transmitido#1;2";
                        mn_field('hselect', 'ED', 'VType', $_POST['VType'], $Qur, '', '', '');
                        mn_field('button', 'ED', 'TView', 'Ver', '', '', '', '" onclick="
		document.TRANSProperties.action=\'_response/call.php?get=Operative/viewdomaintransferresult\';
		subform(\'TRANSProperties\',\'Subcontents\'); ');
                        echo ' | <b>Comprovativo</b> Assunto: ';
                        mn_field('text', 'ED', 'sysMailTopic', '', '100', '40', '', '');
                        $Qur = "Entregar por Email;Receber por Email;Ambos#1;2;3";
                        mn_field('hselect', 'ED', 'sysMailReceivers', '', $Qur, '', '', '');
                        ?></td>
                </tr>
            </table>
            <table width="100%"  border="0" cellpadding="5" cellspacing="1">
                <tr align="center" bgcolor="#F8F8F8">
                    <td>Ordem</td>
                    <td>C&acirc;mbio</td>
                    <td>Taxa</td>
                    <td>Enviado</td>
                    <td>A Pagar</td>
                    <td>Data</td>
                    <td>Tipo</td>
                    <td>Cliente</td>
                    <td><?php if ($_POST['TRANSTYPE'] == 1) {
                            echo 'Correspondente';
                        }; ?></td>
                            <td>Funcionário</td>
                            <td>Estado</td>
                            <td><?php mn_field('checkbox', 'ED', 'checkALL', '', '', '', '', '" onclick="GridCheckAll();'); ?></td>
                        </tr><?php
                        $Origens = $MYT->db_array("SELECT OriNumero, OriNombre FROM origenes
				WHERE OriDominio = (SELECT OriDominio FROM origenes
					WHERE OriActivo = 1 AND OriNumero = {$_SESSION[$SESSCD]}
					AND (OriDominio != 100 AND OriDominio != OriNumero))
				OR OriNumero = {$_SESSION[$SESSCD]}
				AND OriActivo = 1");

                        if ($_POST['View'] == 2 && $_POST['TRANSVIEW'] != '') {
                            $QCorr = " AND FraCorresponsal = " . $_POST['TRANSVIEW'];
                        } elseif ($_POST['View'] == 1) {
                            $QCorr = " AND BnfPais = " . $_POST['TRANSVIEW'];
                        } else {
                            $QCorr = " AND CorrCashier = " . $_POST['TRANSVIEW'];
                        }
                        if ($_POST['VType'] == 1) {
                            if ($_POST['TRANSTYPE'] == 1) {
                                $QrModT = 'FraAlbaran = 0';
                            } elseif ($_POST['TRANSTYPE'] == 2) {
                                $QrModT = 'malote IS NULL';
                            }
                        } else {
                            if ($_POST['TRANSTYPE'] == 1) {
                                $QrModT = 'FraAlbaran > 0';
                            } elseif ($_POST['TRANSTYPE'] == 2) {
                                $QrModT = 'malote > 0';
                            }
                        }

                        if ($_POST['TRANSTYPE'] == 1) {

                            $DBVIEW = "SELECT `FraID`,
  TIME(`FraFechaIngreso`) AS `FraFechaIngreso`,
  `FraCorresponsal`,
  `CorrAlias`,
  `FraReferenciaAg`,
  `FraAlbaran`,
  `FraOrigen1`,
  `FraMonCobro`,
  `FraMonDPago`,
  `FraEnviado`,
  `FraCargo`,
  `FraUrgente`,
  `FraARecibir`,
  `FraTasa`,
  `sg_Abreviatura` AS FraStatus,
  `main_users`.`name` AS FraOrigen2,
  	`CliNombre`
   FROM Facturas
	INNER JOIN Clientes ON CliNumero = FraCliNumero
	INNER JOIN beneficiarios ON BnfNumero = FraBnfNumero
	INNER JOIN hd_statusfacturas ON sg_ID = FraStatus
	INNER JOIN corresponsales ON CorrNumero = FraCorresponsal
	INNER JOIN systemoffice.main_users ON main_users.ref = FraOrigen2
   WHERE FraOrigen0 = {$_SESSION[$SESSCD]}
   {$QCorr} AND FraStatus = 2 AND FraFechaIngreso LIKE '{$_POST['TTime']}%'
   AND {$QrModT}
   	ORDER BY FraOrigen1, FraID";
                        } elseif ($_POST['TRANSTYPE'] == 2) {

                            $VIEW = 'exchange_operations.ref';
                            $QurView = 'exchange_operations.ExcCaxTime';

                            $DBVIEW = "SELECT {$VIEW} AS FraID,
	 					SUM(tax1*Amount)/SUM(Amount) AS FraTasa,
						SUM(exchange_other.value) AS FraCargo,
						SUM(Amount) AS FraEnviado,
						ExcOrigen1 AS FraOrigen1,
						MAX(main_users.name) AS FraOrigen2,
						SUM(payed) AS FraARecibir,
						TIME({$QurView}) AS FraFechaIngreso,
						Time AS FraFechaIngreso,
						MAX(exchange_optype.TYPE) AS FraUrgente,
			 			exchange_operations.in_currency_ref AS FraMonCobro,
			 			exchange_operations.out_currency_ref AS FraMonDPago,
						MAX(exchange_status.est_Short) AS FraStatus,
						malote AS FraAlbaran,
						CliNombre,
						'' AS CorrAlias
   FROM exchange_operations
   INNER JOIN clientes AS regc ON regc.CliNumero = main_clients_ref
   INNER JOIN systemoffice.main_users ON main_users.ref = RefUser
   INNER JOIN exchange_status ON exchange_status.ID = Status
   INNER JOIN exchange_other ON exchange_other.ref = exchange_operations.tax2
   INNER JOIN exchange_optype ON exchange_optype.REF = exchange_operations.type_ref
   WHERE ExcOrigen0 = {$_SESSION[$SESSCD]} AND {$QrModT} AND in_currency_ref = {$_POST['TRANSVIEW']}
   AND Status = 2
   GROUP BY exchange_operations.ref, {$VIEW}, FraMonDPago
   ORDER BY FraOrigen1, FraID ASC";
                        }
                        $a = 1;
                        $GO = $MYT->db_array($DBVIEW);

                        for ($g = 1; $g <= $GO[0]; $g++) {
                            if ($Current_Origen != $GO[$g]['FraOrigen1']) {
                                for ($b = $a; $b <= $Origens[0]; $b++) {
                                    if ($Origens[$b]['OriNumero'] == $GO[$g]['FraOrigen1']) {
                                        $Current_Origen = $GO[$g]['FraOrigen1'];
                                        // Aritm Vars
                                        $AVGRate = 0;
                                        $SUMTax = 0;
                                        $SUMSent = 0;
                                        $SUMReceiv = 0;
                                        echo '<tr>
				<td height="22" colspan="10" bgcolor="#F8F8F8"><b><i>' . $Origens[$b]['OriNombre'] . ' (' . $Origens[$b]['OriNumero'] . ')</i></b></td>
			</tr>';
                                        $a = $b;
                                        $b = $Origens[0];
                                    }
                                }
                            }
                            $rf++;
                            echo '<tr height="25" bgcolor="' . gridlines($g) . '">
        <td NOWRAP>';
                            if ($GO[$g]['FraAlbaran'] == 0) {
                                mn_field('text', 'ED', 'ORALBS[]', $GO[$g]['FraID'], '8', '8', 'False', '" readonly="');
                            } else {
                                echo $GO[$g]['FraID'];
                            }
                            echo '</td>
        <td NOWRAP align="right">' . number_format($GO[$g]['FraTasa'], 4) . '</td>
        <td NOWRAP align="right">' . number_format($GO[$g]['FraCargo'], 2) . '</td>
        <td NOWRAP align="right">' . number_format($GO[$g]['FraEnviado'], 2) . $_SESSION[$SESSST . 'CURS'][$GO[$g]['FraMonCobro']]['CurLabel'] . '</td>
        <td NOWRAP align="right">' . number_format($GO[$g]['FraARecibir'], 2) . $_SESSION[$SESSST . 'CURS'][$GO[$g]['FraMonDPago']]['CurLabel'];
                            if ($GO[$g]['FraAlbaran'] == 0) {
                                mn_field('hidden', 'ED', 'Out', $GO[$g]['FraARecibir'], '', '', '', '');
                                mn_field('hidden', 'ED', 'In', $GO[$g]['FraEnviado'], '', '', '', '');
                            }
                            echo '</td>
        <td NOWRAP align="center">' . $GO[$g]['FraFechaIngreso'] . '</td>
        <td NOWRAP>';
                            if ($GO[$g]['FraUrgente'] == 1) {
                                echo 'URG';
                            } elseif (!is_numeric($GO[$g]['FraUrgente'])) {
                                echo $GO[$g]['FraUrgente'];
                            } echo '</td>
        <td NOWRAP>' . $GO[$g]['CliNombre'] . '</td>
        <td NOWRAP>' . $GO[$g]['CorrAlias'] . '</td>
        <td NOWRAP>' . $GO[$g]['FraOrigen2'] . '</td>
        <td NOWRAP>' . $GO[$g]['FraStatus'] . '</td>';
                            if ($GO[$g]['FraAlbaran'] == 0) {
                                echo '<td NOWRAP>';
                                mn_field('checkbox', 'ED', 'check', $GO[$g]['FraID'], '', '', '', '" onclick="GridCheck(\'' . ($rf - 1) . '\');');
                                echo '</td>';
                            }
                            echo '</tr>';

                            $SUMTax = $SUMTax + $GO[$g]['FraCargo'];
                            $SUMSent = $SUMSent + $GO[$g]['FraEnviado'];
                            $SUMReceiv = $SUMReceiv + $GO[$g]['FraARecibir'];
                            $AVGRate = $AVGRate + ($GO[$g]['FraTasa'] * $GO[$g]['FraEnviado']);

                            if (!isset($GO[$g + 1]['FraOrigen1']) || $Origens[$a]['OriNumero'] != $GO[$g + 1]['FraOrigen1']) {
                                $TCAR = $g - $PCNT;
                                $PCNT = $g;
                                $TSUMTax = $TSUMTax + $SUMTax;
                                $TSUMSent = $TSUMSent + $SUMSent;
                                $TSUMReceiv = $TSUMReceiv + $SUMReceiv;
                                $TAVGRate = $TAVGRate + $AVGRate;

                                if ($SUMSent > 0) {
                                    $AVGRate = $AVGRate / $SUMSent;
                                } else {
                                    $AVGRate = 0;
                                }

                                $SUMTax = number_format($SUMTax, 2);
                                $SUMSent = number_format($SUMSent, 2);
                                $SUMReceiv = number_format($SUMReceiv, 2);
                                $AVGRate = number_format($AVGRate, 4);
                                echo '<tr bgcolor="#7F7F7F"><td height="1" colspan="11"></td>
	</tr>
      <tr bgcolor="#E4E4E4">
        <td># ' . $TCAR . '</td>
        <td align="right">' . $AVGRate . '</td>
        <td align="right">' . $SUMTax . '</td>
        <td align="right">' . $SUMSent . '</td>
        <td align="right">' . $SUMReceiv . '</td>
        <td colspan="6">&nbsp;</td>
        </tr>';
                            }
                        }
                        if ($TSUMSent > 0) {
                            $TAVGRate = $TAVGRate / $TSUMSent;
                        }
                        echo '<tr><td height="2" bgcolor="#000000" colspan="12"></td>
	</tr>
      <tr>
        <td bgcolor="#F8F8F8"><p>Totais: ' . $GO[0] . '</p></td>
        <td align="right" bgcolor="#F8F8F8">';
                        mn_field('text', 'ED', 'AVGRate', number_format($TAVGRate, 4), '19', '5', '', 'INPNUM" disabled readonly="');
                        echo '</td>
        <td align="right" bgcolor="#F8F8F8">';
                        mn_field('text', 'ED', 'SUMTax', number_format($TSUMTax, 2), '10', '3', 'False', 'INPNUM" disabled readonly="');
                        echo '</td>
        <td align="right" bgcolor="#F8F8F8">';
                        mn_field('text', 'ED', 'SUMInvoice', number_format($TSUMSent, 2), '19', '7', 'False', 'INPNUM" disabled readonly="');
                        echo '</td>
        <td align="right" bgcolor="#F8F8F8">';
                        mn_field('text', 'ED', 'SUMReceiv', number_format($TSUMReceiv, 2), '19', '7', 'False', 'INPNUM" disabled readonly="');
                        if (isset($GO[1]['FraMonDPago'])) {
                            echo $_SESSION[$SESSST . 'CURS'][$GO[1]['FraMonDPago']]['CurLabel'];
                        }
                        echo '</td>
        <td colspan="6" bgcolor="#F8F8F8"></td>
        </tr>
	</table>
</form>';